<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Mall;
use app\models\MallGoods;
use app\models\Store;
use app\models\StoreList;
use yii;


class ShopLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getMall($query = [], $page = 1, $limit = 10)
    {
        $mall = Mall::find()
            ->select('m.*,p.partner_name')
            ->from('{{%mall}} m')
            ->leftJoin('{{%partner}} p','p.partner_id=m.partner_id')
            ->filterWhere($query)
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $mall->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $mall->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'mall/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getStoreList($query = [], $page = 1, $limit = 10){
        $store_list = StoreList::find()
            ->filterWhere($query)
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $store_list->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $store_list->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'store/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getStore($id, $page = 1, $limit = 10){
        $store = Store::find()
            ->select('s.*,a1.region_name as province_name,a2.region_name as city_name, a3.region_name as district_name')
            ->from('{{%store}} s')
            ->leftJoin('{{%area}} a1','a1.region_code=s.province')
            ->leftJoin('{{%area}} a2','a2.region_code=s.city')
            ->leftJoin('{{%area}} a3','a3.region_code=s.district')
            ->where(['store_list_id'=>$id])
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $store->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $store->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'store/index',
                'query'=>[]
            ]
        ];
        return $return;
    }

    public function getStoreByStoreList($id, $query=[], $page = 1, $limit = 10){
        if(!$id && $query['store_list_id'])
            $id = $query['store_list_id'];
        $store = Store::find()
            ->select('s.*,a1.region_name as province_name,a2.region_name as city_name, a3.region_name as district_name')
            ->from('{{%store}} s')
            ->leftJoin('{{%area}} a1','a1.region_code=s.province')
            ->leftJoin('{{%area}} a2','a2.region_code=s.city')
            ->leftJoin('{{%area}} a3','a3.region_code=s.district')
            ->where(['store_list_id'=>$id])
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $store->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $store->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'store/edit-storelist',
                'query'=>['store_list_id'=>$id]
            ]
        ];
        return $return;
    }

    public function addStoreList($params){
        $storeList = new StoreList();
        $storeList->list_name = $params['list_name'];
        if($params['list_link'])
            $storeList->list_link = $params['list_link'];
        $storeList->list_remark = $params['list_remark'];
        $storeList->save();
        if(!$params['list_link']){
            $storeList->list_link = \Yii::$app->params['domain']['all_domain'].'?store_list='.$storeList->store_list_id;
            $storeList->save();
        }
        return true;
    }

    public function getStoreListById($id){
        return StoreList::find()->where(['store_list_id'=>$id])->asArray()->one();
    }

    public function editStoreListById($id, $params){
        if(!$storeList = StoreList::findOne($id)) return false;
        $storeList->list_name = $params['list_name'];
        $storeList->list_link = $params['list_link'];
        $storeList->list_remark = $params['list_remark'];
        $storeList->save();
        return true;
    }

    public function deleteStoreList($id){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $storeList = StoreList::findOne($id);
        /*if(Store::findAll(['store_list_id'=>$id]))
            return false;*/
        $result1 = true;
        $store = Store::findAll(['store_list_id'=>$id]);
        foreach($store AS $value){
            $result1 &= $value->delete();
        }
        $result2 = $storeList->delete();
        if($result1 && $result2){
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }
    }

    public function addStore($id,$params){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $store = new Store();
        $store->store_name = $params['store_name'];
        if(isset($params['industry']) && $params['industry'])
            $store->industry = $params['industry'];
        $store->province = $params['province'];
        $store->city = $params['city'];
        $store->district = $params['district'];
        $store->address = $params['address'];
        if(isset($params['longitude']) && $params['longitude'])
            $store->longitude = $params['longitude'];
        if(isset($params['latitude']) && $params['latitude'])
            $store->latitude = $params['latitude'];
        if(isset($params['store_pic']) && $params['store_pic'])
            $store->store_pic = $params['store_pic'];
        $store->store_list_id = $id;
        $result1 = $store->save();
        $storeList = StoreList::findOne($id);
        $storeList->store_num += 1;
        $result2 = $storeList->save();
        if($result1 && $result2){
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }
    }

    public function getStoreById($id){
        return Store::find()->where(['store_id'=>$id])->asArray()->one();
    }

    public function editStoreById($id, $params){
        if(!$store = Store::findOne($id)) return false;
        $store->store_name = $params['store_name'];
        $store->industry = $params['industry'];
        $store->province = $params['province'];
        $store->city = $params['city'];
        $store->district = $params['district'];
        $store->address = $params['address'];
        $store->longitude = $params['longitude'];
        $store->latitude = $params['latitude'];
        return $store->save();
    }

    public function deleteStore($id){
        $store = Store::findOne($id);
        return $store->delete();
    }

    public function addMall($params,$uploadresult1,$uploadresult2){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $mall = new Mall();
        $mall->mall_name = $params['mall_name'];
        $mall->partner_id = $params['partner_id'];
        if($uploadresult1)
            $mall->mall_logo = $uploadresult1;
        $mall->mall_style = $params['mall_style'];
        $mall->mall_color = $params['mall_color'];
        $mall->hide_title = isset($params['hide_title'])?$params['hide_title']:0;
        if($uploadresult2)
            $mall->adv_pic = $uploadresult2;
        $mall->adv_link = $params['adv_link'];
        $time = date('Y-m-d');
        $mall->add_time = $time;
        $mall->update_time = $time;
        $result1 = $mall->save();
        $mall->mall_link = \Yii::$app->params['domain']['all_domain'].'?mall_id='.$mall->mall_id;
        $result2 = $mall->save();
        if($result1 && $result2){
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }
    }

    public function getMallGoodsById($id, $query=[], $page = 1, $limit = 10){
        if(!$id && $query['mall_id'])
            $id = $query['mall_id'];
        $mallGoods = MallGoods::find()
            ->select('g.*,c.category_name,mg.sort')
            ->from('{{%mall_goods}} mg')
            ->leftJoin('{{%goods}} g','g.goods_id=mg.goods_id')
            ->leftJoin('{{%category}} c','c.category_id=g.category_id')
            ->where(['mg.mall_id'=>$id,'g.goods_status'=>1,'g.is_del'=>0])
            ->orderBy('mg.sort')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $mallGoods->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $mallGoods->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'mall/edit',
                'query'=>['mall_id'=>$id]
            ]
        ];
        return $return;
    }

    public function getMallById($id){
        return Mall::find()->where(['mall_id'=>$id])->asArray()->one();
    }

    public function editMallById($id, $params,$uploadresult1,$uploadresult2){
        $mall = Mall::findOne($id);
        $mall->mall_name = $params['mall_name'];
        $mall->partner_id = $params['partner_id'];
        if($uploadresult1)
            $mall->mall_logo = $uploadresult1;
        $mall->mall_style = $params['mall_style'];
        $mall->mall_color = $params['mall_color'];
        $mall->hide_title = isset($params['hide_title'])?$params['hide_title']:0;
        if($uploadresult2)
            $mall->adv_pic = $uploadresult2;
        $mall->adv_link = $params['adv_link'];
        $time = date('Y-m-d');
        $mall->update_time = $time;
        return $mall->save();
    }

    public function saveMallStatus($id){
        $mall = Mall::findOne($id);
        if($mall->mall_status)
            $mall->mall_status = 0;
        else
            $mall->mall_status = 1;
        return $mall->save();
    }

    public function addGoods($mall_id,$goods_id){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $mallGoods = new MallGoods();
        $mallGoods->mall_id = $mall_id;
        $mallGoods->goods_id = $goods_id;
        $result1 = $mallGoods->save();
        $mall = Mall::findOne($mall_id);
        $mall->goods_num += 1;
        $result2 = $mall->save();
        if($result1 && $result2){
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }
    }

    public function deleteGoods($mall_id,$goods_id){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $mallGoods = MallGoods::findOne(['mall_id'=>$mall_id,'goods_id'=>$goods_id]);
        $result1 = $mallGoods->delete();
        $mall = Mall::findOne($mall_id);
        $mall->goods_num -= 1;
        $result2 = $mall->save();
        if($result1 && $result2){
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }
    }

    public function sortGoods($mall_id,$goods_id,$sort){
        $mallGoods = MallGoods::findOne(['mall_id'=>$mall_id,'goods_id'=>$goods_id]);
        $mallGoods->sort = $sort;
        return $mallGoods->save();
    }

}
